<?php
/*
 * 站内信
 * @package    pre
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 625188393a0876d25bf255f4c3c4433f3d739df8 $
 */
class Inbox {

	/**
	 * 发送站内信
	 * @param        $touser
	 * @param        $title
	 * @param        $content
	 * @param int    $typeid
	 * @param string $fromuser
	 * @return bool
	 */
	public static function send($touser, $title, $content, $typeid = 4, $fromuser = '') {
		global $db, $DT_TIME, $DT_IP;
		$tableMember = Table::name(Table::NAME_MEMBER);
		$tableInbox  = Table::name(Table::NAME_MESSAGE);
		if ($touser == $fromuser) return false;
		if (User::checkname($touser) && $title && $content) {
			$title   = addslashes($title);
			$content = addslashes($content);
			$r       = $db->getOne("SELECT black FROM {$tableMember} WHERE username='$touser'");
			if ($r) {
				if ($r['black'] && $typeid != 4) {
					$blacks = explode(' ', $r['black']);
					$_from  = $fromuser ? $fromuser : 'Guest';
					if (in_array($_from, $blacks)) return false;
				}
				$db->query("INSERT INTO {$tableInbox} (title,typeid,touser,fromuser,content,addtime,ip,status) VALUES ('$title', $typeid, '$touser','$fromuser','$content','$DT_TIME','$DT_IP',3)");
				$db->query("UPDATE {$tableMember} SET message=message+1 WHERE username='$touser'");
				if ($fromuser) {
					$db->query("INSERT INTO {$tableInbox} (title,typeid,content,fromuser,touser,addtime,ip,status) VALUES ('$title','$typeid','$content','$fromuser','$touser','$DT_TIME','$DT_IP','2')");
				}
				return true;
			}
		}
		return false;
	}
}